package com.chidopi.market.bookstore.service.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.chidopi.cdf.dao.IBaseDao;
import com.chidopi.cdf.exception.CdfException;
import com.chidopi.cdf.service.IBaseService;
import com.chidopi.market.bookstore.domain.Book2City;
import com.chidopi.market.bookstore.domain.BookCities;
import com.chidopi.market.bookstore.domain.City2Category;
import com.chidopi.market.bookstore.domain.Subscribe4iOS;
import com.chidopi.market.bookstore.domain.Subscriptions;


public class BookCitiesServiceImpl implements IBaseService<BookCities> {
	
	private static Logger logger = LoggerFactory.getLogger(BookCitiesServiceImpl.class);
	
	@Autowired
	@Qualifier("bookCitiesDao")
	private IBaseDao<BookCities> baseDao;
	
	
	@Autowired
	@Qualifier("book2cityDao")
	private IBaseDao<Book2City> book2cityDao;
	
	@Autowired
	@Qualifier("city2categoryDao")
	private IBaseDao<City2Category> city2categoryDao;
	
//	@Autowired
//	@Qualifier("subscribe4iOSDao")
//	private IBaseDao<Subscribe4iOS> iosDao;
	
	@Autowired
	@Qualifier("subscriptionsDao")
	private IBaseDao<Subscriptions> subDao;
	
	@Override
	public int insert(BookCities t) throws Exception {
		return this.baseDao.insert(t);
	}

	@Override
	public int update(BookCities t) throws Exception {
		return this.baseDao.update(t);
	}

	@Override
	public int delete(BookCities t) throws Exception {
		Integer id = t.getId();
		List<Subscriptions> subList = this.subDao.find(" a.CITY_ID = ? ", id);
		if(null != subList && subList.size() > 0){
			logger.debug("delete [" + id + "] failure: Subscriptions.size()=" + subList.size());
			throw new CdfException("error.bookcity.delete.subscriptions");
		}
		
//		List<Subscribe4iOS> ios = this.iosDao.find(" a.CITY_ID = ? ", id);
//		if (ios.size() > 0) {
//			logger.debug("delete [" + id + "] failure: Subscribe4iOS.size()=" + ios.size());
//			throw new CdfException("error.bookcity.delete.subscribe4ios");
//		}			
		
		return this.baseDao.delete(t);
	}

	@Override
	public List<BookCities> find(String sqlWhere, Object... args) throws Exception {
		logger.debug(sqlWhere);
		logger.debug(Arrays.asList(args).toString());		
		return this.baseDao.find(sqlWhere, args);
	}

	@Override
	public BookCities findById(Object id) throws Exception {
		BookCities city = this.baseDao.findById(id);

		if(city != null){			
    		// City2Category
			String sqlWhere = " a.CITY_ID = ? ";
			List<City2Category> categories =  this.city2categoryDao.find(sqlWhere, id);
			city.setCategories(categories);
			
			// books
			List<Book2City> books = this.book2cityDao.find(sqlWhere, id);
			city.setBooks(books);
		}
		return city;
	}

	@Override
	public int getNextSequence() throws Exception {
		return 0;
	}

}
